package com.project.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.SupBean;
import com.project.service.ISupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.yaml.snakeyaml.events.Event;

import java.util.List;

@RestController
@RequestMapping("sup")
public class SupController {
    @Autowired
    private ISupService supService;


    @RequestMapping("cutByItem")
    public IPage<SupBean> cutByItem (Integer pageNO, String name, String type ){
        return supService.cutByItem(pageNO, name, type);
    };
    @RequestMapping("findById")
    public SupBean findById(Integer id){
        return  supService.findById(id);
    };


    @RequestMapping("update")
    public String update(Integer id ,String person,String phone){
        supService.update(id, person, phone);
        return "ok";
    };


    @RequestMapping("add")
    public String add(SupBean supBean){
        supService.add(supBean);
        return "ok";
    };


    @RequestMapping("addList")
    public String addList(@RequestParam("excelFile") MultipartFile mf)throws Exception{
        ImportParams importParams= new ImportParams();
        //标题栏占的行数
        importParams.setTitleRows(1);
        //表头占的行数
        importParams.setHeadRows(1);
        //表示选项卡
        importParams.setSheetNum(1);
        //读取上传excel文件数据，构建为实体List集合；
        List<SupBean> list =   ExcelImportUtil.importExcel(mf.getInputStream(),
                SupBean.class,importParams);
        supService.addList(list);
        return "ok";
    }

    @RequestMapping("findAll")
    public List<SupBean> findAll(){
       return supService.findAll();
    };

}
